<?php
require_once('include/inc_database.php');
require_once('include/inc_useraccount.php');
require_once('include/inc_recommendation.php');
require_once('include/inc_emailsend.php');


// Open DB connection.
$dbobj = new DatabaseObject();
$dbobj->OpenConnection();

$auth = new UserAccount();

$username = $_POST['username'];
$description = trim($_POST['description']);
$artist = trim($_POST['artist']);
$album = trim($_POST['album']);
$song = trim($_POST['song']);

$recText = Recommendation::GetRecText($artist, $album, $song);

// Check for dumb, evil robots.
//echo $_POST['robot_detector'];
if ($_POST['robot_detector'] != "abcfed") {
	echo "<p>Error.  Please enable Javascript.";
	exit();
}

if ($username !== $auth->AuthenticatedUser()) {
	echo "<p>Not logged in.  Must log in to post a recommendation.";
	exit();
}


if (strlen($artist) < 3) {
	echo "<p>Artist text is too short.";
	exit();
}

if (strlen($description) > 2000) {
	echo "<p>Description text is too long (" . strlen($description) . ", max. 2000).";
	exit();
}


$recID = $dbobj->GetFirstCell("SELECT IFNULL(MAX(recID)+1,1) FROM mr_recommendation");
$dbobj->SendQuery("INSERT INTO mr_recommendation (recID, username, recText, artist, album, song, description, addedDateTime, ipAddedBy) VALUES (" . $recID . ", " . ValToSQL_str($username) . ", " . ValToSQL_str($recText) . ", " . ValToSQL_str($artist) . ", " . ValToSQL_str($album) . ", " . ValToSQL_str($song) . ", " . ValToSQL_str($description) . ", '" . date("Y-m-d H:i:s", time()) . "', '" . $_SERVER['REMOTE_ADDR'] . "')");


//---------------------------------
// Send email to email subscribers.
//---------------------------------
$emlsub_ar = $dbobj->GetResultsAsNestedArray("SELECT sub.username, usr.email " .
	"FROM mr_subscription sub " .
	"INNER JOIN mr_useraccount usr ON sub.username = usr.username " .
	"WHERE subscribedToUsername = " . ValToSQL_str($username) . " " .
	"AND usr.email <> '' AND usr.emailNewRecs = 1", 200);

if (sizeof($emlsub_ar) > 0) {

	$email = new EmailSend();

	for ($i=0; $i<sizeof($emlsub_ar); $i++) {
		$email->AddRecipient($emlsub_ar[$i]["email"], "bcc");
	}

	$email->subject = SiteConst('site_name') . " - New rec: " . $artist;
	$email->from_email_address = "musicrecommend@plurib.us";

	$email->body_text = "Your " . SiteConst('site_name') . " subscription feed has been updated.  " . 
		
	$email->body_text .= "\n\nNew recommendation from " . $username . ":";
	$email->body_text .= "\nhttp://plurib.us/musicrecommend/rec.php?recID=" . $recID . "";

	$email->body_text .= "\n\nArtist: " . htmlentities($artist);
	if ($album != "")
		$email->body_text .= "\nAlbum: " . htmlentities($album);
	if ($song != "")
		$email->body_text .= "\nSong: " . htmlentities($song);

	$email->body_text .= "\n\n--";
	$email->body_text .= "\nDo not respond to this email.  To stop receiving these notifications, uncheck \"Email me subscription feed updates\" in your " . SiteConst('site_name') . " account settings.";
	$email->body_text .= "\nhttp://plurib.us/musicrecommend/";

	$email->SendEmail();
}


Header("Location: user.php?user=" . $username . "&tab=out");
exit();
?>

